package cn.core.test.modules.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.core.test.dao.mapper.Co_testMapper;
import cn.core.test.dao.po.Co_testPO;
import cn.core.test.modules.service.Co_testService;
import cn.osworks.aos.core.asset.AOSJson;
import cn.osworks.aos.core.asset.AOSUtils;
import cn.osworks.aos.core.asset.WebCxt;
import cn.osworks.aos.core.dao.SqlDao;
import cn.osworks.aos.core.excel.POIExcelUtil;
import cn.osworks.aos.core.typewrap.Dto;
import cn.osworks.aos.core.typewrap.Dtos;
import cn.osworks.aos.core.typewrap.impl.HashDto;

/**
 * <b>自动生成测试[co_test] controller</b>
 * <p>
 * </p>
 * 
 * @author penghe
 * @date 2016-08-12 16:12:47
 */
@Controller
@RequestMapping(value = "core/co_test")
public class Co_testController {
	@Autowired
	private Co_testService co_testservice;
	@Autowired
	private SqlDao sqlDao;
	@Autowired
	private Co_testMapper co_testmapper;

	/**
	 * 自动生成测试 页面初始化
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "init")
	public String init(HttpSession session, HttpServletRequest request) {
		return "core/test/co_test.jsp";
	}

	/**
	 * 查询自动生成测试
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "listCo_test")
	public void listCo_test(HttpServletRequest request, HttpServletResponse response) {
		Dto inDto = Dtos.newDto(request);
		HttpSession session = request.getSession();
		if (session != null)
			session.setAttribute("co_testsessionid", inDto);
		// sql字定义查询
		// List<Co_testPO> list = sqlDao.list("Co_test.listCo_test", inDto);
		// 标准查询
		List<Co_testPO> list = co_testmapper.likePage(inDto);
		String outString = AOSJson.toGridJson(list, inDto.getPageTotal());
		WebCxt.write(response, outString);
	}

	/**
	 * 保存自动生成测试
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "saveCo_test")
	public void saveCo_test(HttpServletRequest request, HttpServletResponse response) {
		Dto dto = Dtos.newDto(request);
		dto.put("option_name", dto.getUserInfo().getName_());
		dto.put("option_id", dto.getUserInfo().getAccount_());
		dto.put("option_datetime", AOSUtils.getDateTimeStr());
		Dto outDto = co_testservice.saveCo_test(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}

	/**
	 * 更新自动生成测试
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateCo_test")
	public void updateCo_test(HttpServletRequest request, HttpServletResponse response) {
		Dto dto = Dtos.newDto(request);
		dto.put("option_name", dto.getUserInfo().getName_());
		dto.put("option_id", dto.getUserInfo().getAccount_());
		dto.put("option_datetime", AOSUtils.getDateTimeStr());
		Dto outDto = co_testservice.updateCo_test(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}

	/**
	 * 删除自动生成测试
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deleteCo_test")
	public void deleteCo_test(HttpServletRequest request, HttpServletResponse response) {
		Dto dto = Dtos.newDto(request);
		Dto outDto = co_testservice.delete(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}

	/**
	 * 导出自动生成测试
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "exportexcel")
	public void exportexcelCo_test(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws IOException {
		Dto dto = (HashDto) session.getAttribute("co_testsessionid");
		String filename = "";
		try {
			filename = URLEncoder.encode("co_test", "utf-8");
			InputStream bis = null;
			BufferedInputStream buffis = null;
			OutputStream fos = null;
			BufferedOutputStream bos = null;
			fos = response.getOutputStream();
			bos = new BufferedOutputStream(fos);
			String[] titleHead = new String[] { "ID", "名称", "地址", "出生日期", "性别", "使用次数", "钱多多", "操作人名称", "操作人ID",
					"操作时间", };
			String[] datarow = new String[] { "id_", "name_", "add_", "birth_date", "sex_", "usetimes_", "money_",
					"option_name", "option_id", "option_datetime", };
			List<Co_testPO> list = co_testmapper.likePage(dto);
			List<Dto> outlist = new ArrayList<Dto>();
			for (int i = 0; i < list.size(); i++) {
				Co_testPO co_testPO = list.get(i);
				Dto dataDto = new HashDto();
				AOSUtils.copyProperties(co_testPO, dataDto);
				outlist.add(dataDto);
			}
			bis = POIExcelUtil.create(filename, outlist, titleHead, datarow);
			buffis = new BufferedInputStream(bis);
			response.setContentType("application/xls");
			response.setHeader("Content-Disposition",
					"inline;filename=" + URLEncoder.encode(filename + ".xls", "utf-8"));
			int bytesRead = 0;
			byte[] buffer = new byte[1024];
			while ((bytesRead = buffis.read(buffer, 0, buffer.length)) != -1) {
				bos.write(buffer, 0, bytesRead);
			}
			bos.flush();
			bis.close();
			buffis.close();
			fos.flush();
			fos.close();
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
